package com.ld.common.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.ld.common.util.systeminit.SystemInitModel;
import com.ld.common.util.systeminit.SystemInitUtil;
import com.ld.shieldsb.common.core.util.StringUtils;

/**
 * 
 * druid配置，访问地址 druid/index.html
 * 
 * @ClassName DruidStatConfig
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2018年4月4日 下午12:00:25
 *
 */
@Configuration
@ConditionalOnProperty(prefix = "shieldsb", name = "druidStatEnabled", havingValue = "true", matchIfMissing = true)
@Order(2)
public class DruidStatConfig {

    /**
     * 
     * 2、配置一个web监控的filter，注册DruidFilter拦截
     * 
     * @Title duridFilter
     * @author 吕凯
     * @date 2018年12月7日 上午9:59:08
     * @return FilterRegistrationBean<WebStatFilter>
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> duridFilter() {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions",
                "*.css,*.js,*.png,*.jpg,*.gif,*.jpeg,*.bmp,*.ico,*.swf,*.psd,*.htc,*.htm,*.html,*.crx,*.xpi,*.exe,*.ipa,*.apk,*.otf,*.eot,*.svg,*.ttf,*.woff,"
                        + "/druid/*");
        // 用于session监控页面的用户名显示 需要登录后主动将username注入到session里
        filterRegistrationBean.addInitParameter("principalSessionName", "session_user.userName");
        return filterRegistrationBean;
    }

    /**
     * 
     * //1、配置一个管理后台的Servlet，注册DruidServlet
     * 
     * @Title druidServlet
     * @author 吕凯
     * @date 2018年12月7日 上午9:58:57
     * @return ServletRegistrationBean<StatViewServlet>
     */
    @Bean
    public ServletRegistrationBean<StatViewServlet> druidServlet() {
        ServletRegistrationBean<StatViewServlet> registration = new ServletRegistrationBean<>(new StatViewServlet());
        registration.addUrlMappings("/druid/*");
        SystemInitModel initModel = SystemInitUtil.readObjFromConfigFile();
        String allowIp = "127.0.0.1";
        String loginUsername = "kevin";
        String loginPassword = "authdruid";
        if (initModel != null && initModel.isInit()) {
            if (StringUtils.isNotEmpty(initModel.getDruidUserName())) {
                loginUsername = initModel.getDruidUserName();
                loginPassword = initModel.getDruidPassword();
            }
        }
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("resetEnable", "false"); // 禁用HTML页面上的“Reset All”功能
        initParameters.put("allow", allowIp); // ip白名单（没有配置或者为空，则允许所有访问）
        initParameters.put("loginUsername", loginUsername); // ++监控页面登录用户名
        initParameters.put("loginPassword", loginPassword); // ++监控页面登录用户密码
        initParameters.put("deny", ""); // ip黑名单
        // 如果某个ip同时存在，deny优先于allow
        registration.setInitParameters(initParameters);
        return registration;
    }

}
